//
//  APIsMethodRowCellView.swift
//  MacDemo
//
//  Created by FH on 2020/12/16.
//  Copyright © 2020 FH. All rights reserved.
//

import Foundation

@objc
class APIsMethodRowCellView : NSTableCellView, FLTableRowCellable {
    let context = FLContext()
    var methodNode: AstNode?
    var bgView: NSView! = nil
    let lblOrder = FLLabel()
    let lblMethod = FLLabel()
    let lblPlatformLimit = FLLabel()
    let lblTagOrder = FLLabel()
    let lblTagScene = FLLabel()
    let lblTagGroup = FLLabel()
    let lblTagDeprecated = FLLabel()
    
    // REMARK: FLRowCellable
    
    static func cellHeight(by dataItem: Any) -> CGFloat {
        return 45
    }
    
    func updateData(_ data: Any) {
        if let item = data as? AstNode {
            self.lblOrder.flText = "[\(item.order)]"
            let isStaticMethod = item.value?.isStaticValue ?? false
            self.lblMethod.flText = "\(isStaticMethod ? "+" : "-") \(item.name)"
            if self.lblMethod.flText.count > 40 {
                self.lblMethod.font = NSFont.boldSystemFont(ofSize: 12)
            } else {
                self.lblMethod.font = NSFont.boldSystemFont(ofSize: 14)
            }
            
            let platformLimit = item.tags[AstNodeTagType.platformLimit.rawValue] ?? ""
            self.lblPlatformLimit.flText = platformLimit
            
            let orderTag = item.tags[AstNodeTagType.callAPIOrder.rawValue] ?? ""
            self.lblTagOrder.flText = orderTag.count > 0 ? orderTag : "all"
            
            let sceneTag = item.tags[AstNodeTagType.callAPIScene.rawValue] ?? ""
            self.lblTagScene.flText = sceneTag.count > 0 ? sceneTag : "main"
            
            self.lblTagGroup.flText = item.tags[AstNodeTagType.grouping.rawValue] ?? ""
            
            self.lblTagDeprecated.flText = item.tags[AstNodeTagType.available.rawValue] ?? ""
        }
    }
    
    func render(_ table: NSTableView) {
        if self.bgView == nil {
            self.bgView = NSView()
            bgView.bindAsRoot(to: context) { it in
                it.width = table.bounds.size.width.toFLValue()
                it.height = 45
            }
            
            let divTags = NSView()
            divTags.bind(to: context, bgView) { it in
                it.marginTop = 4
                it.marginLeft = 8
                it.direction = .row
                it.height = 20
                it.alignItems = .center
            }
            
            self.lblPlatformLimit.bind(to: context, divTags) { it in
                it.marginRight = 6
                it.height = 14
            }
            self.lblPlatformLimit.font = NSFont.systemFont(ofSize: 11)
            self.lblPlatformLimit.textColor = .white
            self.lblPlatformLimit.flBackgroundColor = .red
            
            self.lblTagGroup.bind(to: context, divTags) { it in
                it.marginRight = 6
                it.height = 14
            }
            self.lblTagGroup.font = NSFont.systemFont(ofSize: 11)
            self.lblTagGroup.textColor = .darkGray
            self.lblTagGroup.flBackgroundColor = .lightGray
            
            self.lblTagOrder.bind(to: context, divTags) { it in
                it.marginRight = 6
                it.height = 14
            }
            self.lblTagOrder.font = NSFont.systemFont(ofSize: 12)
            self.lblTagOrder.textColor = .darkGray
            self.lblTagOrder.flBackgroundColor = .lightGray
            
            self.lblTagScene.bind(to: context, divTags) { it in
                it.marginRight = 6
                it.height = 14
            }
            self.lblTagScene.font = NSFont.systemFont(ofSize: 12)
            self.lblTagScene.textColor = .darkGray
            self.lblTagScene.flBackgroundColor = .lightGray
            
            self.lblTagDeprecated.bind(to: context, divTags) { it in
                it.height = 14
            }
            self.lblTagDeprecated.font = NSFont.systemFont(ofSize: 11)
            self.lblTagDeprecated.textColor = .white
            self.lblTagDeprecated.flBackgroundColor = .red
            
            // method name
            let divMethod = NSView()
            divMethod.bind(to: context, bgView) { it in
                it.direction = .row
                it.alignItems = .center
            }
            
            self.lblOrder.bind(to: context, divMethod) { it in
                it.marginLeft = 2
            }
            self.lblOrder.font = NSFont.systemFont(ofSize: 12)
            self.lblOrder.textColor = .lightGray
            
            self.lblMethod.bind(to: context, divMethod) { it in
                it.height = 20
                it.alignSelf = .stretch
            }
            self.lblMethod.font = NSFont.boldSystemFont(ofSize: 14)
            self.lblMethod.textColor = .darkGray
            self.lblMethod.flCornerRadius = 2
            
            let line = NSView()
            line.bind(to: context, self.bgView) { it in
                it.height = 0.5
                it.position = .absolute
                it.bottom = 0
                it.left = 8
                it.right = 8
            }
            line.flBackgroundColor = .lightGray
            
            context.apply()
            self.addSubview(self.bgView)
        } else {
            context.getNode(by: lblPlatformLimit)?.markDirty()
            context.getNode(by: lblTagOrder)?.markDirty()
            context.getNode(by: lblTagScene)?.markDirty()
            context.getNode(by: lblTagGroup)?.markDirty()
            context.getNode(by: lblTagDeprecated)?.markDirty()
            context.getNode(by: lblOrder)?.markDirty()
            context.getNode(by: lblMethod)?.markDirty()
            context.apply()
        }
    }
    
    func startMouseOver() {
        self.bgView.flBackgroundColor = .lightGray
    }
    
    func stopMouseOver() {
        self.bgView.flBackgroundColor = .white
    }
}
